package etxt2db.uima.cpe;


import java.io.IOException;

import org.apache.uima.cas.CAS;
import org.apache.uima.cas.CASException;
import org.apache.uima.collection.CollectionException;
import org.apache.uima.collection.CollectionReader_ImplBase;
import org.apache.uima.examples.SourceDocumentInformation;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.DocumentAnnotation;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Progress;
import org.apache.uima.util.ProgressImpl;

/**
 * A simple collection reader that reads a document from a text. It can be
 * configured with the following parameters:
 * <ul>
 * <li><code>InputText</code> - the text to use</li>
 * <li><code>Encoding</code> (optional) - character encoding of the input text</li>
 * <li><code>Language</code> (optional) - language of the input document</li>
 * </ul>
 * 
 * 
 */
public class TextCollectionReader extends CollectionReader_ImplBase {
  /**
   * Name of configuration parameter that must be set to the text to process.
   */
  public static final String PARAM_TEXT = "InputText";

  /**
   * Name of configuration parameter that contains the character encoding used by the input text.
   * If not specified, the default system encoding will be used.
   */
  public static final String PARAM_ENCODING = "Encoding";

  /**
   * Name of optional configuration parameter that contains the language of the document
   * correspondent to the input text.
   * If specified this information will be added to the CAS.
   */
  public static final String PARAM_LANGUAGE = "Language";

  private String mText;
  
  private String mLanguage;
  
  private int mCurrentIndex;

  /**
   * @see org.apache.uima.collection.CollectionReader_ImplBase#initialize()
   */
  public void initialize() throws ResourceInitializationException {
    mText = (String) getConfigParameterValue(PARAM_TEXT);
    mLanguage  = (String) getConfigParameterValue(PARAM_LANGUAGE);
    mCurrentIndex = 0;
  }

  /**
   * @see org.apache.uima.collection.CollectionReader#hasNext()
   */
  public boolean hasNext() {
    return mCurrentIndex < 1;
  }

  /**
   * @see org.apache.uima.collection.CollectionReader#getNext(org.apache.uima.cas.CAS)
   */
public void getNext(CAS aCAS) throws IOException, CollectionException {
    JCas jcas;
    try {
      jcas = aCAS.getJCas();
    } catch (CASException e) {
      throw new CollectionException(e);
    }

    mCurrentIndex++;
    // put document in CAS
    jcas.setDocumentText(mText);

    // set language if it was explicitly specified as a configuration parameter
    if (mLanguage != null) {
      ((DocumentAnnotation) jcas.getDocumentAnnotationFs()).setLanguage(mLanguage);
    }

    // Also store location of source document in CAS. This information is critical
    // if CAS Consumers will need to know where the original document contents are located.
    // For example, the Semantic Search CAS Indexer writes this information into the
    // search index that it creates, which allows applications that use the search index to
    // locate the documents that satisfy their semantic queries.
    SourceDocumentInformation srcDocInfo = new SourceDocumentInformation(jcas);
    srcDocInfo.setOffsetInSource(0);
    srcDocInfo.setDocumentSize((int) mText.length());
    srcDocInfo.setLastSegment(mCurrentIndex == 1);
    srcDocInfo.addToIndexes();
  }

  /**
   * @see org.apache.uima.collection.base_cpm.BaseCollectionReader#close()
   */
  public void close() throws IOException {
  }

  /**
   * @see org.apache.uima.collection.base_cpm.BaseCollectionReader#getProgress()
   */
  public Progress[] getProgress() {
    return new Progress[] { new ProgressImpl(mCurrentIndex, 1, Progress.ENTITIES) };
  }

}
